/////////////////////////////////////////////////////////////////////////////
// Original code from libhdfs3. Copyright (c) 2013 - 2014, Pivotal Inc.
// All rights reserved. Author: Zhanwei Wang
/////////////////////////////////////////////////////////////////////////////
//  Modifications by Kumo Inc.
// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//



#pragma once


#include "DataTransferProtocol.h"
#include <kmhdfs/network/socket.h>

/**
 * Version 28:
 *    Declare methods in DataTransferProtocol interface.
 */
#define DATA_TRANSFER_VERSION 28

namespace Hdfs {
namespace Internal {

enum DataTransferOp {
    WRITE_BLOCK = 80,
    READ_BLOCK = 81,
    READ_METADATA = 82,
    REPLACE_BLOCK = 83,
    COPY_BLOCK = 84,
    BLOCK_CHECKSUM = 85,
    TRANSFER_BLOCK = 86,
    REQUEST_SHORT_CIRCUIT_FDS = 87,
    RELEASE_SHORT_CIRCUIT_FDS = 88
};

/**
 * Transfer data to/from datanode using a streaming protocol.
 */
class DataTransferProtocolSender: public DataTransferProtocol {
public:
    DataTransferProtocolSender(Socket & sock, int writeTimeout,
                               const std::string & datanodeAddr);

    virtual ~DataTransferProtocolSender();

    /**
     * Read a block.
     *
     * @param blk the block being read.
     * @param blockToken security token for accessing the block.
     * @param clientName client's name.
     * @param blockOffset offset of the block.
     * @param length maximum number of bytes for this read.
     */
    virtual void readBlock(const ExtendedBlock & blk, const Token & blockToken,
                           const char * clientName, int64_t blockOffset, int64_t length);

    /**
     * Write a block to a datanode pipeline.
     *
     * @param blk the block being written.
     * @param blockToken security token for accessing the block.
     * @param clientName client's name.
     * @param targets target datanodes in the pipeline.
     * @param source source datanode.
     * @param stage pipeline stage.
     * @param pipelineSize the size of the pipeline.
     * @param minBytesRcvd minimum number of bytes received.
     * @param maxBytesRcvd maximum number of bytes received.
     * @param latestGenerationStamp the latest generation stamp of the block.
     */
    virtual void writeBlock(const ExtendedBlock & blk, const Token & blockToken,
                            const char * clientName, const std::vector<DatanodeInfo> & targets,
                            int stage, int pipelineSize, int64_t minBytesRcvd,
                            int64_t maxBytesRcvd, int64_t latestGenerationStamp,
                            int checksumType, int bytesPerChecksum);

    /**
     * Transfer a block to another datanode.
     * The block stage must be
     * either {@link BlockConstructionStage#TRANSFER_RBW}
     * or {@link BlockConstructionStage#TRANSFER_FINALIZED}.
     *
     * @param blk the block being transferred.
     * @param blockToken security token for accessing the block.
     * @param clientName client's name.
     * @param targets target datanodes.
     */
    virtual void transferBlock(const ExtendedBlock & blk,
                               const Token & blockToken, const char * clientName,
                               const std::vector<DatanodeInfo> & targets);

    /**
     * Get block checksum (MD5 of CRC32).
     *
     * @param blk a block.
     * @param blockToken security token for accessing the block.
     * @throw HdfsIOException
     */
    virtual void blockChecksum(const ExtendedBlock & blk,
                               const Token & blockToken);

    /**
     * Request short circuit access file descriptors from a DataNode.
     *
     * @param blk             The block to get file descriptors for.
     * @param blockToken      Security token for accessing the block.
     * @param maxVersion      Maximum version of the block data the client
     *                          can understand.
     */
    virtual void requestShortCircuitFds(const ExtendedBlock blk,
                                        const Token& blockToken,
                                        uint32_t maxVersion);

private:
    Socket & sock;
    int writeTimeout;
    std::string datanode;
};

}
}

